/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * RTVRijnmond Note: 
 * - Single pages use gif?page_00
 * - Multi pages use gif?page_01
 * The above result in the first page. 
 * We can't be sure about what item to take, so a bit of sub page 
 * shuffling will be needed when using RTV Rijnmond Text.
 */

#include "Teletext.h"
#include "RTVRijnmondTeletext.h"

#include <QtDebug>

const QString RTVRijnmondTeletext::TEXTNAME("RTVRijnmondTeletext");

/**
 * \short Constructor of the base Teletext object. 
 * 
 * Constructor of the base Teletext object. 
 */

RTVRijnmondTeletext::RTVRijnmondTeletext(PageInfo *p) {
  channelname = RTVRijnmondTeletext::TEXTNAME;

  pageinfo = p;
  qnam = new QNetworkAccessManager(this);

  notfound = NULL;

  QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(imageNotFound(QNetworkReply*)));
}

/**
 * \short Destructor of the base Teletext object. 
 * 
 * Destructor of the base Teletext object. 
 */

RTVRijnmondTeletext::~RTVRijnmondTeletext() {

}

/**
 * Return a pixmap to the current page
 */

void RTVRijnmondTeletext::getTextPage(int page, int subpage) {
  firsttry = true;
  _page = page;
  _subpage = subpage;

  QString url;
  QNetworkRequest request;

  if(notfound == NULL) {
    url.sprintf("http://80.95.161.141/tt.gif?%d_%02d", 1000, 1);
  } else {
    if(subpage == 1) {
      url.sprintf("http://80.95.161.141/tt.gif?%d_%02d", page, subpage - 1);
    } else {
      url.sprintf("http://80.95.161.141/tt.gif?%d_%02d", page, subpage);
    }
  }
  request.setUrl(QUrl(url));
  request.setRawHeader("User-Agent", USER_AGENT);
  reply = qnam->get(request);

}

/**
 * \short Slot function for a finished download
 * 
 * \param p Pointer to the QNetworkReply
 * 
 * Set the pixpage to the downloaded image. 
 * This function needs to be overridden if the download is not an image. 
 */

void RTVRijnmondTeletext::downloadFinished(QNetworkReply* reply) {
  qDebug() << "RTVRijnmond::downloadFinished() - url: " << reply->request().url();
  QImage image;
  if (reply->error() == QNetworkReply::NoError) {
    // Valid reply
    image.load(reply, 0);
    if (!image.isNull()) {
      pixpage = new QPixmap(QPixmap::fromImage(image));
      if(pixpage != NULL) {
	QImage fail = notfound->toImage();
	QImage down = pixpage->toImage();
	if(down != fail) {
	  pageinfo->setTeletextPage(pixpage);
	  qDebug() << "RTVRijnmond::downloadFinished(): OK";
	} else {
	  if(firsttry) {
	    firsttry = false;
	    qDebug() << "RTVRijnmond::downloadFinished(): download +1";

	    QString url;
	    url.sprintf("http://80.95.161.141/tt.gif?%d_%02d", _page, _subpage);

	    QNetworkRequest request;
	    request.setUrl(QUrl(url));
	    request.setRawHeader("User-Agent", USER_AGENT);
	    reply = qnam->get(request);
	    return;
	  } else {
	    pageinfo->setTeletextPage(pixpage);
	  }
	}
      }
    }
  }
  else {
    // Page not found
      qDebug() << "RTVRijnmond::downloadFinished(): ERR";
  }
  reply->deleteLater();
}

/**
 * \short Slot function for a not found image donwload
 * 
 * \param p Pointer to the QNetworkReply
 * 
 * Sets the pointer to the not found image
 */

void RTVRijnmondTeletext::imageNotFound(QNetworkReply* reply) {
  qDebug() << "RTVRijnmondTeletext::imageNotFound() - url: " << reply->request().url();
  QImage image;
  if (reply->error() == QNetworkReply::NoError) {
    // Valid reply
    image.load(reply, 0);
    if (!image.isNull()) {
      notfound = new QPixmap(QPixmap::fromImage(image));
      qDebug() << "RTVRijnmondTeletext::imageNotFound(): Set";
    }
  }
  else {
    // Page not found
      qDebug() << "RTVRijnmondTeletext::imageNotFound(): ERR";
  }
  //reply->deleteLater();
  // Disconnect the current callback
  QObject::disconnect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(imageNotFound(QNetworkReply*)));
  // Set the normal callback
  QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
}
